home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 695 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.1 KB

  1. From: jamshid@io.com (Jamshid Afshar)
  2. Message-ID: <4i4ooa$l5d@xanadu.io.com>
  3. X-Original-Date: 12 Mar 1996 15:02:02 -0600
  4. Path: in2.uu.net!bounce-back
  5. Date: 13 Mar 96 04:50:53 GMT
  6. Approved: fjh@cs.mu.oz.au
  7. Newsgroups: comp.std.c++
  8. Subject: Constructor vs conversion operator in ambiguity resolution
  9. Organization: Illuminati Online, Austin, Texas, USA
  10. Summary: please verify I found a compiler bug
  11. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  12.     iQBFAgUBMUZUO+EDnX0m9pzZAQGZ4AF/XLZRIS6cMV/wl/qTEkUmkxQC2n5pUIzS
  13.     38mezOhI69eYmt+qDMVY37Ivakng3/b5
  14.     =W1fO
  15.  
  16. I just want to verify that the below code is an ambiguity error.  VC++
  17. 4.0 accepts the code.  The rules haven't changed recently in this
  18. regard, have they?  Btw, g++ 2.6.3 also accepts the code, but it calls
  19. operator<<(ostream&, const char*).
  20.  
  21. // VC++ should be flagging the "cout << cs;" line below as an
  22. // ambiguity error because converting a CString to a "const char*" is
  23. // an equivalent match to using it to construct a temporary Foo.  But,
  24. // VC++ silently accepts and chooses to construct a temporary Foo.
  25. // A workaround is to define an operator<<(ostream&,const CString&),
  26. // which really should be defined by MFC anyway.
  27.  
  28. #include <iostream.h> // defines operator<<( ostream&, const char* )
  29.  
  30. class CString {
  31.     const char* _s;
  32. public:
  33.     CString( const char* s ) : _s(s) {}  // obviously just for demonstration
  34.     operator const char*() const { return _s; }
  35. };
  36.  
  37. class Foo {
  38. public:
  39.     Foo( const CString& ) {}
  40.     friend ostream& operator<<( ostream& os, const Foo& f )
  41.     { return os << "This is a Foo." << endl; }
  42. };
  43.  
  44. int main() {
  45.     CString cs = "this is a CString";
  46.     cout << cs;  // error: ambiguous, but VC++ silently accepts using Foo ctor
  47.     cout << endl;
  48.     return 0;
  49. }
  50. ---
  51. [ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
  52. [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
  53. [ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
  54. [ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
  55. [ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
  56.